package com.app.service;

import com.alibaba.fastjson.JSON;
import com.app.dao.mysql.YjDataMapper;
import com.app.domain.UploadParameter;
import com.app.domain.YjData;
import com.app.domain.jgaeb.LoginCookie;
import com.app.domain.jgaeb.PostGrouting;
import com.app.domain.jgaeb.PostGroutingResult;
import com.app.domain.jgaeb.PostGroutings;
import com.app.domain.yxtj.YxtjResultJson;
import com.app.utils.HttpClients;
import com.app.utils.MathUtil;
import com.app.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

@Slf4j
@Service
@Transactional
public class JgaebService {
    @Autowired
    UploadParameterService uploadParameterService;
    @Autowired
    YjDataService yjDataService;
    @Autowired
    YjDataMapper yjDataMapper;

    public PostGrouting getPostGrouting(List<YjData> yjDataList, UploadParameter uploadParameter, Map<String, Object> map) {
        PostGrouting grouting = new PostGrouting();
        YjData yjData = yjDataList.get(0);
        grouting.setTestDate(yjData.getMudjack_date() + " " + yjData.getBegin_time());
//        grouting.setBeamID(null);
        grouting.setBeamName(yjData.getBridge_code());
        grouting.setBridgename(uploadParameter.getZhbw());
        grouting.setD03(String.valueOf(yjData.getEnvironment_temperature()));
        grouting.setD04(String.valueOf(yjData.getSlurry_temperature()));
        grouting.setD05(String.valueOf(yjData.getSlurry_temperature()));
        grouting.setD06("0");
        grouting.setD07("");
        grouting.setD08(yjData.getPractical_fluidity());
        grouting.setD09("0");
        grouting.setD10(String.valueOf(yjDataList.size()));
        grouting.setD11("28");
        grouting.setD12("100");
        grouting.setD13("0");
        grouting.setD14("0");
        grouting.setD15("0");
        grouting.setD16("常规");
        grouting.setD17("常规");
        grouting.setD18("常规");
        grouting.setD19("常规");
        grouting.setD20(String.valueOf(yjData.getPractical_mudjack_value()));
        grouting.setD21(String.valueOf(yjData.getPractical_water_value()));
        grouting.setD22("0");
        grouting.setD23("0");
        grouting.setD24("0");
        grouting.setD25("常规");
        grouting.setD26("常规");
        grouting.setD27(String.valueOf(yjData.getWater_binder_ratio()));
        return grouting;
    }

    public PostGroutings getPostGroutings(YjData yjData, UploadParameter uploadParameter, Map<String, Object> map, String parent) {


        PostGroutings groutings = new PostGroutings();
        groutings.setParent(parent);
        groutings.setTestDate(yjData.getMudjack_date());
        groutings.setFileCount(0);
        groutings.setHoleID(yjData.getDuct_code());
        groutings.setD02("左至右");
        groutings.setD03(yjData.getBegin_time().substring(0, 5));
        groutings.setD04(yjData.getEnd_time().substring(0, 5));
        groutings.setD05(String.valueOf(yjData.getMudjack_pressure()));
        groutings.setD06("正常");
        groutings.setD07("正常");
        groutings.setD08("3");
        groutings.setD09(yjData.getPressurize_time());
        groutings.setD10(String.valueOf(yjData.getStabilize_pressure()));
        groutings.setD11("");
        groutings.setD12("");
        groutings.setD13("");
        groutings.setD14("");
        groutings.setD15("");
        groutings.setD16("");
        groutings.setD17("");
        groutings.setD18("");
        groutings.setD19("");
        groutings.setD20("");
        groutings.setD21("");
        groutings.setD22("");
        groutings.setD23("");


        return groutings;

    }

    public PostGroutingResult sendMessage(String serviceUrl, String json, List<LoginCookie> cookies) {
        PostGroutingResult zmqbResult = null;
        try {
            String result = HttpClients.requestPost1(serviceUrl, json, cookies);
            System.out.println(result);
            zmqbResult = (PostGroutingResult) JSON.parseObject(StringUtil.replaceBlank(result), PostGroutingResult.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return zmqbResult;
    }

    public static String getOrderNo() {
        String orderNo = "";
        String trandNo = String.valueOf((Math.random() * 9 + 1) * 1000000);
        String sdf = new SimpleDateFormat("yyyyMMddHHMMSS").format(new Date());
        orderNo = trandNo.toString().substring(0, 4);
        orderNo = orderNo + sdf;
        return orderNo;
    }


    public static String getNum19() {
        String numStr = "";
        String trandStr = String.valueOf((Math.random() * 4 + 1) * 1000);
        String dataStr = new SimpleDateFormat("HHMMSS").format(new Date());
        numStr = trandStr.toString().substring(0, 2);
        numStr = numStr + dataStr;
        return numStr;
    }

    public static void main(String[] args) {
        String num19 = getNum19();
        System.out.println(num19);
    }

    public String save(String deviceCode, String bridgeCode, String ductCode, String mudjackDate, String beginTime, String endTime) {
        YjData yjData = new YjData();
        yjData.setBridge_code(bridgeCode);
        yjData.setDevice_code(deviceCode);
        yjData.setDuct_code(ductCode);
        yjData.setMudjack_date(mudjackDate);
        yjData.setBegin_time(beginTime);
        yjData.setEnd_time(endTime);
        yjData.setMudjack_pressure(MathUtil.getRandomFloat(0.5f, 0.7f));
        yjData.setSlurry_pressure(MathUtil.getRandomFloat(0.5f, 0.7f));
        yjData.setStabilize_pressure(MathUtil.getRandomFloat(0.5f, 0.7f));
        yjData.setPressurize_time("180");
        yjData.setWater_binder_ratio(0.27f);
        yjData.setTheory_volume(230f);
        yjData.setPractical_volume(120f);
        yjData.setPractical_mudjack_value(0f);
        yjData.setPractical_water_value(0f);
        yjData.setEnvironment_temperature(27f);
        yjData.setSlurry_temperature(21f);
        yjData.setPractical_fluidity("15");
        yjData.setRemark("");
        yjData.setMudjack_curve_graph("");
        yjData.setState("2");
        int save = yjDataMapper.save(yjData);
        String s="";
        if (save>0){
            s = "保存成功";
        }else {
            s = "保存失败";
        }


        return s;
    }
}
